package com.wzh.glsc.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.common.utils.R;
import com.wzh.glsc.ware.dao.WareSkuDao;
import com.wzh.glsc.ware.entity.PurchaseDetailEntity;
import com.wzh.glsc.ware.entity.WareSkuEntity;
import com.wzh.glsc.ware.feign.ProductFeignService;
import com.wzh.glsc.ware.service.WareSkuService;
import com.wzh.glsc.ware.to.SkuHasStockTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        String key=(String)params.get("key");
        QueryWrapper<WareSkuEntity> wareSkuEntityQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty((String) params.get("skuId"))) {
            wareSkuEntityQueryWrapper.eq("sku_id", (String) params.get("skuId"));
        }
        if (StringUtils.isNotEmpty((String) params.get("wareId"))) {
            wareSkuEntityQueryWrapper.eq("ware_id", (String) params.get("wareId"));
        }
//        if (StringUtils.isNotEmpty(key)) {
//            wareSkuEntityQueryWrapper.and((obj)->{
//                    obj.eq("sku_id", key)
//                    .or().like("stock", key)
//                    .or().like("sku_name", key)
//                    .or().like("stock_locked", key);
//            });
//        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wareSkuEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void saveWareSkuByPurchaseDetailEntity(PurchaseDetailEntity savePurchaseDetailEntity) {
        WareSkuEntity wareSkuEntity=baseMapper.selectOne(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id", savePurchaseDetailEntity.getSkuId())
                .eq("ware_id", savePurchaseDetailEntity.getWareId())
        );
        if(wareSkuEntity!=null){
            baseMapper.updateWareSkuEntity(savePurchaseDetailEntity.getSkuId(),savePurchaseDetailEntity.getWareId(),savePurchaseDetailEntity.getSkuNum());
        }else {
            WareSkuEntity saveWareSkuEntity=new WareSkuEntity();
            saveWareSkuEntity.setSkuId(savePurchaseDetailEntity.getSkuId());
            saveWareSkuEntity.setWareId(savePurchaseDetailEntity.getWareId());
            saveWareSkuEntity.setStock(savePurchaseDetailEntity.getSkuNum());
            try {
                R r=productFeignService.info(saveWareSkuEntity.getSkuId());
                Map<String,Object> data=(Map<String,Object>)r.get("skuInfo");
                saveWareSkuEntity.setSkuName((String) data.get("skuName"));
            }catch (Exception e){

            }
            save(saveWareSkuEntity);
        }
    }

    @Override
    public List<SkuHasStockTo> hasStock(List<Long> skuIdList) {
//        baseMapper.selectList(new QueryWrapper<WareSkuEntity>().in("", "s"));
        List<WareSkuEntity> wareSkuEntities=baseMapper.wareSkuBySkuIdList(skuIdList);
        List<SkuHasStockTo> collect = wareSkuEntities.stream().map(wareSkuEntity -> {
            SkuHasStockTo skuHasStockTo = new SkuHasStockTo();
            skuHasStockTo.setHasStock(((wareSkuEntity.getStock() != null ? wareSkuEntity.getStock() : 0)
                    - (wareSkuEntity.getStockLocked() == null ? 0 : wareSkuEntity.getStockLocked())) > 0);
            skuHasStockTo.setSkuId(wareSkuEntity.getSkuId());
            return skuHasStockTo;
        }).collect(Collectors.toList());
        return collect;
    }

}